package com.lishangshan.oas_system.controller.plan;

import com.lishangshan.oas_system.model.dao.notedao.AttachmentDao;
import com.lishangshan.oas_system.model.dao.notedao.NoteDao;
import com.lishangshan.oas_system.model.dao.plandao.PlanDao;
import com.lishangshan.oas_system.model.dao.plandao.Planservice;
import com.lishangshan.oas_system.model.dao.system.StatusDao;
import com.lishangshan.oas_system.model.dao.system.TypeDao;
import com.lishangshan.oas_system.model.dao.user.UserDao;
import com.lishangshan.oas_system.model.dao.user.UserService;
import com.lishangshan.oas_system.model.entity.note.Attachment;
import com.lishangshan.oas_system.model.entity.plan.Plan;
import com.lishangshan.oas_system.model.entity.system.SystemStatusList;
import com.lishangshan.oas_system.model.entity.system.SystemTypeList;
import com.lishangshan.oas_system.model.entity.user.User;
import com.lishangshan.oas_system.utils.StringtoDateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/")
public class PlanReprotController {
    @Autowired
    private NoteDao noteDao;
    @Autowired
    private AttachmentDao attDao;
    @Autowired
    PlanDao planDao;
    @Autowired
    Planservice planservice;
    @Autowired
    TypeDao typeDao;
    @Autowired
    StatusDao statusDao;
    @Autowired
    UserDao userDao;
    @Autowired
    UserService userService;
    @Autowired
    AttachmentDao attachmentDao;
    Attachment att;
    List<Plan> pList;
    List<User> uList;
    Date startDate, endDate;
    String choose2;
    Logger log = LoggerFactory.getLogger(getClass());
    // 格式转化导入
    DefaultConversionService service = new DefaultConversionService();

    /**
     * 计划报表面板
     *
     * @param request
     * @param model
     * @param session
     * @param page
     * @param baseKey
     * @return
     */
    @RequestMapping("myplan")
    public String test2(HttpServletRequest request, Model model, HttpSession session,
                        @RequestParam(value = "page", defaultValue = "0") int page,
                        @RequestParam(value = "baseKey", required = false) String baseKey) {
        plantablepaging(request, model, session, page, baseKey);
        return "plan/plantable";
    }

    /**
     * 数据 请求 回显
     * @param request
     * @param model
     * @param session
     * @param page
     * @param baseKey
     */
    //计划报表
    private void plantablepaging(HttpServletRequest request, Model model, HttpSession session, int page,
                                 String baseKey) {
        List<SystemTypeList> type = (List<SystemTypeList>) typeDao.findByTypeModel("aoa_plan_list");
        List<SystemStatusList> status = (List<SystemStatusList>) statusDao.findByStatusModel("aoa_plan_list");
        List<Plan> plans = new ArrayList<>();
        // 利用set过滤掉重复的plan_user_id 因为set不能重复
        Set<Long> number = new HashSet();
        Plan plan2;
        long typeid = 13;
        Long choose;
        service.addConverter(new StringtoDateUtil());
        String starttime = request.getParameter("starttime");
        String endtime = request.getParameter("endtime");
        System.out.println(starttime + ";" + endtime);
        Date start = service.convert(starttime, Date.class);
        Date end = service.convert(endtime, Date.class);
        String choose1 = request.getParameter("choose");
        //分页的时候记住
        if (start == null && end == null && choose1 == null) {
            start = startDate;
            end = endDate;
            choose1 = choose2;
        }
        if (start != null && end != null && choose1 != null) {
            startDate = start;
            endDate = end;
            choose2 = choose1;
        }
        // 1是日计划2是周计划3是月计划
        if (choose1 == null || choose1.length() == 0)
            choose = 1l;
        else
            choose = Long.valueOf(choose1);
        if (choose == 1) {
            typeid = 13l;
        }
        if (choose == 2) {
            typeid = 14l;
        }
        if (choose == 3) {
            typeid = 15l;
        }
        pList = (List<Plan>) planDao.findAll();
        Long userid = Long.valueOf(session.getAttribute("userId") + "");
        Page<User> uListpage = userService.findmyemployuser(page, baseKey, userid);
        for (Plan plan : pList) {
            number.add(plan.getUser().getUserId());
        }
        System.out.println(number);
        // 找到相对应的计划记录
        for (Long num : number) {
            plan2 = planDao.findlatest(start, end, num, typeid);
            if (plan2 != null)
                plans.add(plan2);
        }
        System.out.println("有没有plan" + plans);
        // 将用户名和list绑定在一起
        Map<String, Plan> uMap = new HashMap<>();
        for (User user : uListpage) {
            if (plans.size() == 0)
                uMap.put(user.getUserName(), null);
            for (Plan plan : plans) {
                if (Objects.equals(user.getUserId(), plan.getUser().getUserId())) {
                    uMap.put(user.getUserName(), plan);
                    break;
                } else {
                    uMap.put(user.getUserName(), null);
                }
            }
            System.out.println("map" + uMap);
        }
        System.out.println(uListpage.getContent());

        //记住开始时间和结束时间以及选择
        model.addAttribute("starttime", starttime);
        model.addAttribute("endtime", endtime);
        model.addAttribute("choose", choose1);

        model.addAttribute("uMap", uMap);
        model.addAttribute("type", type);
        model.addAttribute("status", status);
        model.addAttribute("plans", plans);
        model.addAttribute("plist", pList);
        model.addAttribute("ulist", uListpage.getContent());
        model.addAttribute("page", uListpage);
        model.addAttribute("url", "realplantable");
    }

    /**
     * 评论计划
     *
     * @param request
     * @param model
     * @param session
     * @param pid
     * @param comment
     * @param page
     * @param baseKey
     * @return
     */
    @RequestMapping("realplantable")
    public String test23(HttpServletRequest request, Model model, HttpSession session,
                         @RequestParam(value = "pid", required = false) String pid,
                         @RequestParam(value = "comment", required = false) String comment,
                         @RequestParam(value = "page", defaultValue = "0") int page,
                         @RequestParam(value = "baseKey", required = false) String baseKey) {
        System.out.println("pid的" + pid);
        plantablepaging(request, model, session, page, baseKey);
        if (!StringUtils.isEmpty(pid)) {
            Plan plan = planDao.findOne(Long.valueOf(pid));
            if (plan.getPlanComment() == null)
                plan.setPlanComment(comment);
            else
                plan.setPlanComment(plan.getPlanComment() + comment);
            planDao.save(plan);
        }
        return "plan/realplantable";
    }
}
